package dwei.view
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.core.UIComponent;
	
	internal class UIContainer extends UIComponent
	{
//============================================================================
//PROPERTY
//============================================================================
		protected var _layer:uint;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function UIContainer()
		{
			super();
			mouseEnabled = false;
		}
//============================================================================
//OVERRIDE
//============================================================================
		override protected function createChildren():void
		{
			super.createChildren();
			var sprite:Sprite = new Sprite();
			addChild(sprite);
			mask = sprite;
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			var sprite:Sprite = mask as Sprite;
			sprite.graphics.clear();
			sprite.graphics.beginFill(0);
			sprite.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			sprite.graphics.endFill();
			validateLayout(unscaledWidth, unscaledHeight);
		}
//============================================================================
//PROTECTED
//============================================================================
		protected function validateLayout(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var x:int;
			var y:int;
			var ui:IUI;
			var index:int = numChildren - 1;
			while (index > -1)
			{
				ui = getChildAt(index--) as IUI;
				if (ui != null)
				{
					ui.setActualSize(ui.getExplicitOrMeasuredWidth(), ui.getExplicitOrMeasuredHeight());
					x = ui.x;
					switch (ui.horizontalAlign)
					{
						case UISize.LEFT:
							x = 0;
							break;
						case UISize.CENTER:
							x = Math.max(0, (unscaledWidth - ui.width) / 2);
							break;
						case UISize.RIGHT:
							x = unscaledWidth - ui.width;
							break;
					}
					y = ui.y;
					switch (ui.verticalAlign)
					{
						case UISize.TOP:
							y = 0;
							break;
						case UISize.MIDDLE:
							y = Math.max(0, (unscaledHeight - ui.height) / 2);
							break;
						case UISize.BOTTOM:
							y = unscaledHeight - ui.height;
							break;
					}
					ui.move(x, y);
				}
			}
		}
//============================================================================
//getter/setter
//============================================================================
		public function get layer():uint
		{
			return _layer;
		}
		
		public function set layer(value:uint):void
		{
			_layer = value;
		}


	}
}